// Michael Vaganov
// GSP420 example

#include <iostream>
#include <conio.h>

using namespace std;

#define HEIGHT 11
#define WIDTH 13

char picture[HEIGHT][WIDTH] = {
	{'.','L',' ',' ',' ','*','*','*',' ',' ',' ','R','.',},
	{'.',' ',' ',' ',' ','*',' ','*',' ',' ',' ',' ','.',},
	{'.',' ',' ',' ',' ','*',' ','*','*','#',' ',' ','.',},
	{'.',' ',' ',' ',' ','*','*','*',' ','#',' ',' ','.',},
	{'.',' ',' ',' ','#','#','#','#','#',' ',' ',' ','.',},
	{'.',' ',' ','#','#',' ','#',' ',' ',' ',' ',' ','.',},
	{'.',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','.',},
	{'.',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','.',},
	{'.',' ',' ',' ',' ',' ','&','+','+',' ',' ',' ','.',},
	{'.',' ',' ',' ',' ','+',' ',' ','+',' ',' ',' ','.',},
	{'.','L',' ',' ','+','+',' ',' ',' ',' ',' ','R','.',},
};

void moveHeadDown();

int main()
{
	//system("cls");
	//// show how to flip and rotate 2D arrays
	//cout << "normal" << endl;
	//cout << "-------------" << endl;
	//for(int r = 0; r < HEIGHT; ++r)
	//{
	//	for(int c = 0; c < WIDTH; ++c)
	//	{
	//		cout << picture[r][c];
	//	}
	//	cout << endl;
	//}
	//cout << "-------------" << endl;
	//getch();

	////system("cls");
	////cout << "flipped horizontally" << endl;
	////cout << "-------------" << endl;
	////for(int r = 0; r < HEIGHT; ++r)
	////{
	////	for(int c = 0; c < WIDTH; ++c)
	////	{
	////		cout << picture[r][(WIDTH-1)-c];
	////	}
	////	cout << endl;
	////}
	////cout << "-------------" << endl;
	////getch();

	////system("cls");
	////cout << "flipped verticallly" << endl;
	////cout << "-------------" << endl;
	////for(int r = 0; r < HEIGHT; ++r)
	////{
	////	for(int c = 0; c < WIDTH; ++c)
	////	{
	////		cout << picture[(HEIGHT-1)-r][c];
	////	}
	////	cout << endl;
	////}
	////cout << "-------------" << endl;
	////getch();

	////system("cls");
	////cout << "flipped about x/y" << endl;
	////cout << "-------------" << endl;
	////for(int r = 0; r < WIDTH; ++r)		// notice r < WIDTH
	////{
	////	for(int c = 0; c < HEIGHT; ++c)	// notice c < HEIGHT
	////	{
	////		cout << picture[c][r];
	////	}
	////	cout << endl;
	////}
	////cout << "-------------" << endl;
	////getch();

	//system("cls");
	//cout << "rotated CW 90*" << endl;
	//cout << "-------------" << endl;
	//for(int r = 0; r < WIDTH; ++r)		// notice r < WIDTH
	//{
	//	for(int c = 0; c < HEIGHT; ++c)	// notice c < HEIGHT
	//	{
	//		cout << picture[(HEIGHT-1)-c][r];
	//	}
	//	cout << endl;
	//}
	//cout << "-------------" << endl;
	//getch();

	//system("cls");
	//cout << "rotated CCW 90*" << endl;
	//cout << "-------------" << endl;
	//for(int r = 0; r < WIDTH; ++r)		// notice r < WIDTH
	//{
	//	for(int c = 0; c < HEIGHT; ++c)	// notice c < HEIGHT
	//	{
	//		cout << picture[c][(WIDTH-1)-r];
	//	}
	//	cout << endl;
	//}
	//cout << "-------------" << endl;
	//getch();

	//// show how to selectivley modify a 2D array
	//char input;

	//system("cls");

	//do
	//{
	//	// show the stick man image
	//	for(int r = 0; r < HEIGHT; ++r)
	//	{
	//		for(int c = 0; c < WIDTH; ++c)
	//		{
	//			cout << picture[r][c];
	//		}
	//		cout << endl;
	//	}
	//	cout << endl;

	//	// get some user input
	//	input = getch();

	//	// make just the stick man's head fall
	//	moveHeadDown();
	//}
	//while(input != 'q' && input != 27);

	// the size of the 2D array
	const int high = 2, wide = 3;
	// the pointer that will reference the 2D array
	//char ** r;
	//// allocate the height index, which is an array of arrays
	//r = new char * [high];
	//// allocate each row
	//for(int i = 0; i < high; ++i)
	//{
	//	r[i] = new char [wide];
	//}
	// put a value in each element
	//for(int y = 0; y < high; ++y)
	//{
	//	for(int x = 0; x < wide; ++x)
	//	{
	//		r[y][x] = (int)'*';
	//	}
	//}

	char r[high][wide] = {
		{'.', '*', '.'},
		{'*', '*', '*',},
	};

	// print each element (testing purposes)
	//for(int y = 0; y < high; ++y)
	//{
	//	for(int x = 0; x < wide; ++x)
	//	{
	//		char c = (char)r[y][x];
	//		if(c >= 7 && c <= 13)
	//		{
	//			c = '?';
	//		}
	//		cout << c << " ";
	//	}
	//	cout << endl;
	//}

	int high2 = 20, wide2 = 10;
	int ** r2;
	r2 = new int * [high2];
	// allocate each row
	for(int i = 0; i < high2; ++i)
	{
		r2[i] = new int [wide2];
	}
	// copy the old array into the new array
	for(int y = 0; y < high2; ++y)
	{
		for(int x = 0; x < wide2; ++x)
		{
			r2[y][x] = (int)'.';
		}
	}
	for(int y = 0; y < high; ++y)
	{
		for(int x = 0; x < wide; ++x)
		{
			r2[y+3][x+1] = r[y][x];
		}
	}
	// print each element (testing purposes)
	for(int y = 0; y < high2; ++y)
	{
		for(int x = 0; x < wide2; ++x)
		{
			char c = (char)r2[y][x];
			if(c >= 7 && c <= 13)
			{
				c = '?';
			}
			cout << c << " ";
		}
		cout << endl;
	}

	return 0;
}

void moveHeadDown()
{
	int temp;
	// count backwards... 
	// because we want to move the bottom part first
	system("cls");
	for(int row = HEIGHT-1; row >= 0; --row)
	{
		for(int col = 0; col < WIDTH; col++)
		{
			if(picture[row][col] == '*'){
				// bounds checking logic
				if(row+1 >= HEIGHT){
					cout << "CANNOT FALL ANYMORE!" << endl;
					return;
				}

				// collision detection logic
				if(picture[row+1][col] == '&'){
					cout << "SOMTHING IN THE WAY!!" << endl;
					return;
				}
				// move the portion of the head down
				picture[row+1][col] = picture[row][col];

				// and erase the spot where it used to be
				picture[row][col] = ' ';
			}
		}
	}
}

